package com.ruoyi.syf.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.ConvertField;
import com.ruoyi.common.annotation.ConvertFieldItem;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.syf.common.constant.EscalationConfigEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.syf.mapper.EscalationConfigMapper;
import com.ruoyi.syf.domain.EscalationConfig;
import com.ruoyi.syf.service.IEscalationConfigService;

/**
 * 上报配置Service业务层处理
 *
 * @author yr
 * @date 2024-08-26
 */
@Service
public class EscalationConfigServiceImpl extends ServiceImpl<EscalationConfigMapper,EscalationConfig> implements IEscalationConfigService {

    @Autowired
    private EscalationConfigMapper escalationConfigMapper;

    @ConvertField({
            @ConvertFieldItem(fieldName = "type",
                    constantValue = {"0:通用", "1:http", "2:TCPSocket"}),
    })
    @Override
    public List<EscalationConfig> list(EscalationConfig escalationConfig) {
        return escalationConfigMapper.list(escalationConfig);
    }

    @Override
    public EscalationConfig selectById(Long id) {
        return getById(id);
    }

    @Override
    public boolean update(EscalationConfig escalationConfig) {
        EscalationConfig config = getById(escalationConfig.getId());
        config.setConfigValue(escalationConfig.getConfigValue());
        return updateById(config);
    }

    /**
     * 新增公共上报配置
     */
    @Override
    public void saveConfig(EscalationConfigEnum configEnum){
        EscalationConfig escalationConfig = new EscalationConfig();
        escalationConfig.setConfigKey(configEnum.getValue());
        escalationConfig.setRemarks(configEnum.getName());
        escalationConfig.setType(configEnum.getType());
        escalationConfig.setState(true);
        escalationConfig.setSortOrder(configEnum.getSort());
        escalationConfig.setCreateBy(1L);
        escalationConfig.setUpdateBy(1L);
        save(escalationConfig);
    }

    @Override
    public List<EscalationConfig> getRidOfCompanyCode() {
        LambdaQueryWrapper<EscalationConfig> lqw = new LambdaQueryWrapper<>();
        lqw.ne(EscalationConfig::getConfigKey,EscalationConfigEnum.COMPANY_CODE.getValue());
        return list(lqw);
    }

    @Override
    public EscalationConfig getCompanyCode() {
        LambdaQueryWrapper<EscalationConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EscalationConfig::getConfigKey,EscalationConfigEnum.COMPANY_CODE.getValue());
        return getOne(lqw);
    }

}
